<template>
  <div>
    <el-input
      :suffix-icon="Search"
      v-model="dataObj.nameOrCode"
      placeholder="请输入字典名称或字典编码"
      @keydown.enter="handleSearchDictType"
    ></el-input>
    <div class="mt-3.5">
      <el-button type="primary" @click="openAddDialog">
        <el-icon>
          <Plus />
        </el-icon>
        新增字典
      </el-button>
      <el-button type="primary">
        <el-icon>
          <Download />
        </el-icon>
        导出
      </el-button>
    </div>
  </div>
  <div class="table-container mt-3">
    <div
      :class="['table-item', activeId === item.id ? 'active' : '']"
      v-for="item in dictList"
      :key="item.id"
      @click="handleCurrentChange(item)"
    >
      <div class="table-item__label">
        <span>{{ item.dictName }}({{ item.dictCode }})</span>
      </div>
      <div class="table-item__controls">
        <el-button type="text" @click="editItem(item)">
          <el-icon>
            <Edit />
          </el-icon>
        </el-button>
        <el-button :disabled="item.systemFlag === 'SYSTEM'" type="text" @click="deleteItem(item)">
          <el-icon>
            <Delete />
          </el-icon>
        </el-button>
      </div>
    </div>
  </div>
  <dict-add ref="dictAddRef" @close="getList"></dict-add>
</template>

<script setup lang="ts">
import { Edit, Delete, Search } from "@element-plus/icons-vue";
import { Dict } from "@/api/interface/dict";
import { getDictDetail, getDictList, removeDict } from "@/api/modules/dict";
import DictAdd from "@/views/system/dict/component/Add.vue";
import { useComponentRef } from "@/hooks/useComponentRef";
import { ElMessage } from "element-plus";

const dictList = ref<Dict.IDictType[]>([]);
const dictAddRef = useComponentRef(DictAdd);
const dataObj = reactive({
  nameOrCode: ""
});

const activeId = ref<number | string | undefined>("");

const emits = defineEmits(["currentRow"]);

const getList = async () => {
  const { data } = await getDictList(dataObj.nameOrCode);
  dictList.value = data;
  if (dictList.value.length) {
    activeId.value = dictList.value[0].id;
    emits("currentRow", dictList.value[0]);
  }
};

const editItem = async (item: Dict.IDictType) => {
  const { data } = await getDictDetail(item.id);
  dictAddRef.value?.openDialog("edit", data);
};

const deleteItem = async (item: Dict.IDictType) => {
  try {
    await removeDict(item.id);
    await getList();
    ElMessage.success("删除成功");
  } catch (e) {
    ElMessage.error("删除失败");
  }
};

const handleCurrentChange = (row: Dict.IDictType) => {
  activeId.value = row.id ? row.id : -1;
  emits("currentRow", row);
};

const handleSearchDictType = () => {
  getList();
};
const openAddDialog = () => {
  dictAddRef.value?.openDialog();
};

onMounted(() => {
  getList();
});
defineExpose({
  getList
});
</script>

<style scoped lang="scss">
.table-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  .table-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px;
    cursor: pointer;
    background-color: #f5f7fa;
    border-radius: 5px;
    &:hover {
      background-color: var(--el-color-primary-light-9);
    }
    &.active {
      color: var(--el-color-primary-light-1);
    }
    .table-item__label {
      flex: 0 0 70%;
      width: 70%;
      overflow: hidden;
      font-size: 14px;
      font-weight: 400;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .table-item__controls {
      display: flex;
      flex: 0 0 30%;
      justify-content: center;
    }
  }
}
</style>
